// ==========================================================================
// GFast自动生成controller操作代码。
// 生成日期：2024-11-11 21:00:33
// 生成路径: internal/app/system/controller/balance_records.go
// 生成人：王中阳
// desc:余额记录
// company:云南奇讯科技有限公司
// ==========================================================================

package controller

import (
	"context"
	"errors"

	"github.com/gogf/gf/v2/encoding/gurl"
	"github.com/gogf/gf/v2/net/ghttp"
	"github.com/tiger1103/gfast/v3/api/v1/system"
	"github.com/tiger1103/gfast/v3/internal/app/system/model"
	"github.com/tiger1103/gfast/v3/internal/app/system/service"
	"github.com/tiger1103/gfast/v3/library/libUtils"
	"github.com/xuri/excelize/v2"
)

type balanceRecordsController struct {
	BaseController
}

var BalanceRecords = new(balanceRecordsController)

// List 列表
func (c *balanceRecordsController) List(ctx context.Context, req *system.BalanceRecordsSearchReq) (res *system.BalanceRecordsSearchRes, err error) {
	res = new(system.BalanceRecordsSearchRes)
	res.BalanceRecordsSearchRes, err = service.BalanceRecords().List(ctx, &req.BalanceRecordsSearchReq)
	return
}

// Export 导出excel
func (c *balanceRecordsController) Export(ctx context.Context, req *system.BalanceRecordsExportReq) (res *system.BalanceRecordsExportRes, err error) {
	var (
		r        = ghttp.RequestFromCtx(ctx)
		listData []*model.BalanceRecordsInfoRes
		//表头
		tableHead = []interface{}{"ID", "用户", "金额", "当前余额", "时间", ""}
		excelData [][]interface{}
		//字典选项处理
	)
	req.PageNum = 1
	req.PageSize = 500
	//获取字典数据
	excelData = append(excelData, tableHead)
	for {
		listData, err = service.BalanceRecords().GetExportData(ctx, &req.BalanceRecordsSearchReq)
		if err != nil {
			return
		}
		if listData == nil {
			break
		}
		for _, v := range listData {
			var (
				//单选-用户
				userid string
			)
			//关联表-单选-用户
			if v.LinkedUserid != nil {
				userid = v.LinkedUserid.Username
			}
			dt := []interface{}{
				v.Id,
				//关联表-单选-用户
				userid,
				v.Money,
				v.Balance,
				v.CreatedAt.Format("Y-m-d H:i:s"),
				v.UpdatedAt.Format("Y-m-d H:i:s"),
			}
			excelData = append(excelData, dt)
		}
		req.PageNum++
	}
	//创建excel处理对象
	excel := new(libUtils.ExcelHelper).CreateFile()
	defer excel.Close()
	excel.ArrToExcel("Sheet1", "A1", excelData)
	col, _ := excelize.ColumnNumberToName(len(tableHead))
	row := len(excelData)
	cr, _ := excelize.JoinCellName(col, row)
	excel.SetCellBorder("Sheet1", "A1", cr)
	_, err = excel.WriteTo(r.Response.BufferWriter)
	if err != nil {
		return
	}
	r.Response.Header().Set("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
	r.Response.Header().Set("Accept-Ranges", "bytes")
	r.Response.Header().Set("Access-Control-Expose-Headers", "*")
	r.Response.Header().Set("Content-Disposition", "attachment; filename="+gurl.Encode("余额记录")+".xlsx")
	r.Response.Buffer()
	r.Exit()
	return
}

// LinkedBalanceRecordsDataSearch 相关连表查询数据
func (c *balanceRecordsController) LinkedBalanceRecordsDataSearch(ctx context.Context, req *system.LinkedBalanceRecordsDataSearchReq) (res *system.LinkedBalanceRecordsDataSearchRes, err error) {
	if !service.SysUser().AccessRule(ctx, service.Context().GetUserId(ctx), "api/v1/system/balanceRecords/list") {
		err = errors.New("没有访问权限")
		return
	}
	res = new(system.LinkedBalanceRecordsDataSearchRes)
	res.LinkedBalanceRecordsDataSearchRes, err = service.BalanceRecords().LinkedBalanceRecordsDataSearch(ctx)
	return
}

// Get 获取余额记录
func (c *balanceRecordsController) Get(ctx context.Context, req *system.BalanceRecordsGetReq) (res *system.BalanceRecordsGetRes, err error) {
	res = new(system.BalanceRecordsGetRes)
	res.BalanceRecordsInfoRes, err = service.BalanceRecords().GetById(ctx, req.Id)
	return
}

// Add 添加余额记录
func (c *balanceRecordsController) Add(ctx context.Context, req *system.BalanceRecordsAddReq) (res *system.BalanceRecordsAddRes, err error) {
	err = service.BalanceRecords().Add(ctx, req.BalanceRecordsAddReq)
	return
}

// Edit 修改余额记录
func (c *balanceRecordsController) Edit(ctx context.Context, req *system.BalanceRecordsEditReq) (res *system.BalanceRecordsEditRes, err error) {
	err = service.BalanceRecords().Edit(ctx, req.BalanceRecordsEditReq)
	return
}

// Delete 删除余额记录
func (c *balanceRecordsController) Delete(ctx context.Context, req *system.BalanceRecordsDeleteReq) (res *system.BalanceRecordsDeleteRes, err error) {
	err = service.BalanceRecords().Delete(ctx, req.Ids)
	return
}
